/*****************************************************************************
 *
 * Microchip DeviceNet Stack (User level Connection Object events)
 *
 *****************************************************************************
 * FileName:        UsrConn.c
 * Dependencies:    
 * Processor:       PIC18F with CAN
 * Compiler:       	C18 02.20.00 or higher
 * Linker:          MPLINK 03.40.00 or higher
 * Company:         Microchip Technology Incorporated
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the "Company") is intended and supplied to you, the Company's
 * customer, for use solely and exclusively with products manufactured
 * by the Company. 
 *
 * The software is owned by the Company and/or its supplier, and is 
 * protected under applicable copyright laws. All rights are reserved. 
 * Any use in violation of the foregoing restrictions may subject the 
 * user to criminal sanctions under applicable laws, as well as to 
 * civil liability for the breach of the terms and conditions of this 
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *
 * This file contains events generated from the Connection Object that cannot
 * be handled internally.
 * 
 *
 *
 * Author               Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Ross Fosler			06/17/03	...	
 * 
 *****************************************************************************/



#include	"dnetdef.h"			// Global definitions file
#include 	"typedefs.h"

#include	"conn.h"			// All globals in the Connection object

#include	"services.h"		// Service codes
#include	"errors.h"			// Error codes
#include	"class.h"			// Class codes

#include	"route.h"			// Global symbols defined by the Router Obj


//lifeng 2022-08
#include "dnet_userIF.h"

//lifeng 2023-04
//IO 路径
unsigned char pollMsgConsPath[6]={
    0x20,0x04,0x24,0x64,0x30,0x03
};

unsigned char pollMsgProdPath[6]={
    0x20,0x04,0x24,0x65,0x30,0x03
};

/*********************************************************************
 * Function:        unsigned char UsrConnCreateEvent(unsigned char hInstance)
 *
 * PreCondition:    N/A
 *
 * Input:       	instance no#
 *                  
 * Output:         	status of the request
 *
 * Side Effects:    N/A
 *
 * Overview:        This event occures when a Connection Creation has been 
 *					requested. 
 *
 * Note:            The application programmer should utilize this event
 *					to allocate resources for the request, i.e. hardware
 *					peripherals and RAM. If there is a resource problem
 *					then the function should return 0 to prevent the
 *					connection from opening consiquently notfying the 
 *					master on the network.
 *
 *					Valid instances are INST_POLL, INST_STROBE, 
 *					INST_CYC_COS, and INST_MULTICAST.
 ********************************************************************/
unsigned char UsrConnCreateEvent(unsigned char hInstance)
{
	switch (hInstance)
	{
		case INST_POLL:
			// Set the Class trigger
			
			// Set the path and path length
		    flag_nsled=1;                            //LED lifeng 2022-12
		    uConn2.attrib.consumed_path_len.word = 6;
		    uConn2.attrib.consumed_path = pollMsgConsPath;
		    uConn2.attrib.produced_path_len.word = 6;
		    uConn2.attrib.produced_path = pollMsgProdPath;
			
			// Set the connection size
		    // lifeng 2022-12-7
			uConn2.attrib.consumed_con_size.word = POLL_CSM_LEN;
			uConn2.attrib.produced_con_size.word = POLL_PRU_LEN;
			
			// Set the pointers to the buffer
			uConn2.rx.pMsg = pollINBuffer;
			uConn2.tx.pMsg = pollOUTBuffer;
			return(INST_POLL);
			
		// Add handling for other static instances if necessary
		case INST_STROBE:
		case INST_CYC_COS:
		case INST_MULTICAST:					
		default:
			return(NULL);	// Do not allow creation
	}
}



/*********************************************************************
 * Function:        void UsrConnCloseEvent(unsigned char hInstance)
 *
 * PreCondition:    N/A
 *
 * Input:       	instance no#
 *                  
 * Output:         	none
 *
 * Side Effects:    N/A
 *
 * Overview:        This event occures when a Connection Close has been 
 *					requested. 
 *
 * Note:            Release any resource that were allocated from creation.
 *
 *					Valid instances are INST_POLL, INST_STROBE, 
 *					INST_CYC_COS, and INST_MULTICAST.
 ********************************************************************/
void UsrConnCloseEvent(unsigned char hInstance)
{
	switch (hInstance)
	{
		case INST_POLL:
		    flag_nsled=2;                            //LED lifeng 2022-12
			return;
			
		// Add other static instances if necessary
		// case INST_STROBE:
		// case INST_CYC_COS:
		// case INST_MULTICAST:					
	}
	
	return;
}



/*********************************************************************
 * Function:        void UsrConnRxDataEvent(unsigned char hInstance)
 *
 * PreCondition:    N/A
 *
 * Input:       	instance no#
 *                  
 * Output:         	none
 *
 * Side Effects:    N/A
 *
 * Overview:        This event occures when data has been received for
 *					a particular Connection instance. 
 *
 * Note:            This event does not need to be handled. Internal 
 *					Connection Object Services can be used to determine 
 *					the presence of new data.
 *
 *					Valid instances are INST_POLL, INST_STROBE, 
 *					INST_CYC_COS, and INST_MULTICAST.
 ********************************************************************/
void UsrConnRxDataEvent(unsigned char hInstance)
{
	// Use the public connection services or poll for the event

/*	
	sswitch (hInstance)
	{
		case INST_POLL:
						
			break;
						
		// Add other static instances if necessary
		// case INST_STROBE:
		// case INST_CYC_COS:
		// case INST_MULTICAST:					
		
	}
	
	return;
	}
*/
}


/*********************************************************************
 * Function:        void UsrConnTxDataEvent(unsigned char hInstance)
 *
 * PreCondition:    N/A
 *
 * Input:       	instance no#
 *                  
 * Output:         	none
 *
 * Side Effects:    N/A
 *
 * Overview:        This event occures when data has been transmitted for
 *					a particular Connection instance. 
 *
 * Note:            This event does not need to be handled. Internal 
 *					Connection Object Services can be used to determine 
 *					the completion of transmission.
 *
 *					Valid instances are INST_POLL, INST_STROBE, 
 *					INST_CYC_COS, and INST_MULTICAST.
 ********************************************************************/
void UsrConnTxDataEvent(unsigned char hInstance)
{
	// Use the public connection services or poll for the event
/*	
	switch (hInstance)
	{
		case INST_POLL:
						
			break;
			
		// Add other static instances if necessary
		// case INST_STROBE:
		// case INST_CYC_COS:
		// case INST_MULTICAST:					
		
	}
	
	return;
*/
}



/*********************************************************************
 * Function:        void UsrConnSetAttribEvent(unsigned char hInstance)
 *
 * PreCondition:    N/A
 *
 * Input:       	instance no#
 *                  
 * Output:         	none
 *
 * Side Effects:    N/A
 *
 * Overview:        This event occures when a Set Attribute has been 
 *					requested for a particular instance that cannot
 *					be handled internally because of some application
 *					dependency. 
 *
 * Note:            Valid instances are INST_EXPL, INST_POLL, 
 *					INST_STROBE, INST_CYC_COS, and INST_MULTICAST.
 ********************************************************************/
void UsrConnSetAttribEvent(unsigned char hInstance)
{
	// Use the public router services to determine the specifics
	
	switch (hInstance)
	{
		case INST_POLL:
			switch(mRouteGetAttributeID())
			{
				case CONN_ATTRIB_CLASS_TRIGGER:
					break;
				case CONN_ATTRIB_PRODUCED_CONN_PATH:	
					break;
				case CONN_ATTRIB_CONSUMED_CONN_PATH:
					break;
				case CONN_ATTRIB_CONSUMED_CONN_SIZE:
					break;
			}
			
			break;
			
		// Add other static instances if necessary
		// case INST_EXPL:
		// case INST_POLL:
		// case INST_STROBE:
		// case INST_CYC_COS:
		// case INST_MULTICAST:					
		default:
			return;	
	}
}
							










